package me.firstapp.bts;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import me.firstapp.bts.service.AccountJobService;
import me.firstapp.bts.service.MemberJobService;
import me.firstapp.bts.service.OrderJobService;
import me.firstapp.bts.service.TransferJobService;

public class JobTask {

	private static Logger logger = LoggerFactory.getLogger(JobTask.class);

	@Autowired
	private OrderJobService orderJobService;

	@Autowired
	private AccountJobService accountJobService;

	@Autowired
	private MemberJobService memberJobService;

	@Autowired
	private TransferJobService transferJobService;

	// 订单过期任务执行
	private ExecutorService orderExpireExecutorService = Executors.newFixedThreadPool(1);

	// 订单结算任务执行
	private ExecutorService orderSettleExecutorService = Executors.newFixedThreadPool(1);

	// 积分结算贝币任务执行
	private ExecutorService settleWeibiToJinbiExecutorService = Executors.newFixedThreadPool(1);

	// 粉丝数量更新任务
	private ExecutorService fansCountsExecutorService = Executors.newFixedThreadPool(1);

	// 检查过期
	private ExecutorService checkExpireExecutorService = Executors.newFixedThreadPool(1);

	// 下架超时转让
	private ExecutorService offExpireTransferExecutorService = Executors.newFixedThreadPool(1);

	// 新增转让用户
	private ExecutorService addTransferMemberExecutorService = Executors.newFixedThreadPool(1);

	/**
	 * 未支付订单过期处理 10分钟
	 */
	public synchronized void doOrderExpireTask() {
		orderExpireExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doOrderExpireTask(..) start ... ");
				orderJobService.cancelExpireOrders();
				logger.info("doOrderExpireTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}

	/**
	 * 结算订单任务处理 1分钟
	 */
	public synchronized void doOrderSettleTask() {
		orderSettleExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doOrderSettleTask(..) start ... ");
				orderJobService.settleOrders();
				logger.info("doOrderSettleTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}

	/**
	 * 积分结算贝币任务处理00:05开始
	 */
	public synchronized void doSettleWeibiToJinbiTask() {
		settleWeibiToJinbiExecutorService.execute(new Runnable() {
			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doSettleWeibiToJinbiTask(..) start ... ");
				accountJobService.settleWeiBiToJinBi();
				logger.info("doSettleWeibiToJinbiTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}

	/**
	 * 下架超时转让 02:00开始
	 */
	public synchronized void doOffTransferTask() {
		offExpireTransferExecutorService.execute(new Runnable() {

			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doOffTransferTask(..) start ... ");
				transferJobService.offTransferMemberProduct();
				logger.info("doOffTransferTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}

	/**
	 * 粉丝数量更新任务 04:00开始
	 */
	public synchronized void doFansCountsTask() {
		fansCountsExecutorService.execute(new Runnable() {

			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doFansCountsTask(..) start ... ");
				memberJobService.setFansCounts();
				logger.info("doFansCountsTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}

	/**
	 * 新增转让用户任务 06:00开始
	 */
	public synchronized void doAddTransferMemberTask() {
		addTransferMemberExecutorService.execute(new Runnable() {

			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doAddTransferMemberTask(..) start ... ");
				transferJobService.addAndUpdateTransferMember();
				logger.info("doAddTransferMemberTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}

	/**
	 * 检查过期任务 9:00开始
	 */
	public synchronized void doCheckExpireTask() {
		checkExpireExecutorService.execute(new Runnable() {

			@Override
			public void run() {
				long start = System.currentTimeMillis();
				logger.info("doCheckExpireTask(..) start ... ");
				memberJobService.checkExpire();
				logger.info("doCheckExpireTask(..) end, time cost => " + (System.currentTimeMillis() - start));
			}
		});
	}
}
